local huisheng = fk.CreateSkill {
  name = "tyj_heg__huisheng",
}
local H = require "packages.ol_hegemony.util"
local U = require "packages.utility.utility"

Fk:loadTranslationTable {
  ["tyj_heg__huisheng"] = "贿生",
  [":tyj_heg__huisheng"] = "与你势力相同的角色是你“合纵”的合法目标。当你受到伤害时，你可以展示任意张牌并令伤害来源选择一项：1.获得这些牌并防止本次伤害；2.弃置不少于这些牌数量的手牌,然后你移除此武将牌。",
  ["#tyj_heg__huisheng-invoke"] = "是否展示任意张牌以对 %dest 发动 贿生 ？",
  ["#tyj_heg__huisheng-discard"] = "贿生：弃置至少 %arg 张牌，否则（点取消）获得展示牌并防止伤害。",
  ["$tyj_heg__huisheng1"] = "只要放过我，这些，都是你的。",
  ["$tyj_heg__huisheng2"] = "家有余财，愿尽献将军！",
  ["$tyj_heg__huisheng3"] = "大人，这些钱够吗？",
  ["$tyj_heg__huisheng4"] = "劳烦大人美言几句~",
}
huisheng:addEffect(fk.DetermineDamageInflicted, {
  mute = true,
  anim_type = "defensive",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(huisheng.name) and not player:isNude() and data.from and
        data.from ~= player
        and not data.from.dead
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local cards = room:askToCards(player, {
      skill_name = huisheng.name,
      min_num = 1,
      max_num = 998,
      prompt = "#tyj_heg__huisheng-invoke::" .. data.from.id,
      include_equip = true,
      cancelable = true,
    })
    if #cards > 0 then
      event:setCostData(self, { cards = cards, })
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room  = player.room
    local cards = event:getCostData(self).cards
    player:broadcastSkillInvoke(huisheng.name, math.random(1, 2))
    room:notifySkillInvoked(player, huisheng.name)
    player:showCards(cards)
    local n = #cards
    local todis = room:askToDiscard(data.from, {
      min_num = n + 1,
      max_num = math.max(n + 1, #data.from:getCardIds('h')),
      include_equip = false,
      skill_name = huisheng.name,
      cancelable = true,
      prompt = "#tyj_heg__huisheng-discard:::" .. n + 1,
    })
    if #todis > n then
      local isDeputy = H.inGeneralSkills(player, huisheng.name)
      if isDeputy then
        H.removeGeneral(player, isDeputy == "d")
      end
    else
      room:obtainCard(data.from, cards, true, fk.ReasonPrey, data.from, huisheng.name)
      data:preventDamage()
    end
  end,
})
huisheng:addEffect(fk.BeforeCardsMove, {
  can_refresh = function(self, event, target, player, data)
    if player:hasShownSkill(huisheng.name) then
      for _, move in ipairs(data) do
        if move.from == player and move.to and move.to ~= player and H.compareKingdomWith(move.to, player) and move.skillName == "alliance&" then
          return true
        end
      end
    end
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    player:broadcastSkillInvoke(huisheng.name, math.random(3, 4))
    room:notifySkillInvoked(player, huisheng.name)
  end,
})
huisheng:addEffect("allowAlliance", {
  allow_alliance = function(self, from, to)
    return H.compareKingdomWith(to, from) and from:hasShownSkill(huisheng.name)
  end
})
return huisheng
